package com.masterclient.app.modules.shop.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.support.v7.widget.RecyclerView;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.eoner.baselibrary.utils.ScreenUtils;
import com.masterclient.app.R;
import com.masterclient.app.modules.shop.beans.StockChangeBean;
import com.masterclient.app.widget.CenterImageSpan;

public class StockChangeAdapter extends BaseQuickAdapter<StockChangeBean.ChangeBean, BaseViewHolder> {

    private final Context mContext;

    public StockChangeAdapter(Context mContext) {
        super(R.layout.item_stock_change);
        this.mContext = mContext;
    }

    @Override
    protected void convert(BaseViewHolder helper, StockChangeBean.ChangeBean item) {
        View viewItem = helper.getView(R.id.view_item);
        TextView tvName = helper.getView(R.id.tv_name);
        if (!TextUtils.isEmpty(item.stockTypeTitle)){
            setTextTag(tvName, item);
        }else {
            tvName.setText(item.title);
        }
        helper.setText(R.id.tv_stock_num,item.changeCount);
        helper.setText(R.id.tv_sku,item.spec);
        helper.setGone(R.id.tv_sku, !TextUtils.isEmpty(item.spec));
        helper.setText(R.id.tv_order_id,item.bizNo);
        helper.setGone(R.id.tv_order_id, !TextUtils.isEmpty(item.bizNo));
        helper.setText(R.id.tv_time,item.changeTime);
        helper.setGone(R.id.tv_time, !TextUtils.isEmpty(item.changeTime));
        helper.setText(R.id.tv_update_cause,item.reasonTitle);
        helper.setGone(R.id.tv_update_cause, !TextUtils.isEmpty(item.reasonTitle));
        helper.setText(R.id.tv_des,item.reasonDesc);
        helper.setGone(R.id.tv_des, !TextUtils.isEmpty(item.reasonDesc));

        if (helper.getLayoutPosition() == 0){
            RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) viewItem.getLayoutParams();
            params.topMargin = ScreenUtils.dp2px(10);
            viewItem.setLayoutParams(params);
        }
    }

    private void setTextTag(TextView tv,StockChangeBean.ChangeBean item){
        SpannableString spannableString = new SpannableString(item.title);
        TextView tvTag = new TextView(mContext);
        if ("1".equals(item.stockType) || "3".equals(item.stockType)){
            tvTag.setBackgroundResource(R.drawable.bg_ff9c3d_tag);
        }else if ("2".equals(item.stockType) || "4".equals(item.stockType)){
            tvTag.setBackgroundResource(R.drawable.bg_ff5858_tag);
        }
        tvTag.setText(item.stockTypeTitle);
        tvTag.setTextSize(10);
        tvTag.setTextColor(mContext.getResources().getColor(R.color.white));
        tvTag.setLayoutParams(new FrameLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ScreenUtils.dp2px(16)));
        tvTag.setGravity(Gravity.CENTER);
        tvTag.setIncludeFontPadding(false);
        tvTag.setPadding(ScreenUtils.dp2px(4),ScreenUtils.dp2px(1),ScreenUtils.dp2px(4),ScreenUtils.dp2px(1));
        tvTag.setPadding(ScreenUtils.dp2px(2),ScreenUtils.dp2px(1),ScreenUtils.dp2px(2),ScreenUtils.dp2px(1));
        tvTag.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED), View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
        tvTag.layout(0, 0, tvTag.getMeasuredWidth(), tvTag.getMeasuredHeight());
        tvTag.buildDrawingCache();
        Bitmap bitmap = tvTag.getDrawingCache();
        Drawable d = new BitmapDrawable(bitmap);
        d.setBounds(0, 0, tvTag.getWidth(), tvTag.getHeight());//缺少这句的话，不会报错，但是图片不回显示
        CenterImageSpan span = new CenterImageSpan(d,0, ScreenUtils.dp2px(4));
        spannableString.setSpan(span, 0, "礼包".length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        tv.setText(spannableString);
        tv.setGravity(Gravity.CENTER_VERTICAL);
    }
}
